//
//  MoreThemeListVIewController.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/22.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "MoreThemeListViewController.h"
#import "ThemeManager.h"

@interface MoreThemeListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSArray *themelistAr;

@property (nonatomic, assign) NSInteger LastSelectedIndex;

@end

@implementation MoreThemeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSUserDefaults *userdefualt = [NSUserDefaults standardUserDefaults];
    _LastSelectedIndex = [userdefualt integerForKey:@"lastSelectedIndex"];
    
    [self creatTableView];
}


/**
 * 懒加载数组themelistAr
 */
- (NSArray *)themelistAr{
    
    if (!_themelistAr) {
        
        /**
         * 1、获取主题包
         */
        NSString *themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
        NSDictionary *themesDic = [NSDictionary dictionaryWithContentsOfFile:themePath];
        
        NSArray *ar = [themesDic allKeys];
        
        _themelistAr = [ar copy];

    }
    
    return _themelistAr;
}


/**
 * 创建tabelView
 */
- (void)creatTableView{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 44) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
}


/**
 * 返回cell的个数
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.themelistAr.count;
}


/**
 * 创建cell
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"MoreThemeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = self.themelistAr[indexPath.row];
    
    if (_LastSelectedIndex == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
}


/**
 * 选择某行,放出通知，切换主题。
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    /**
     * 1、获取主题名
     */
    NSString *themeName = _themelistAr[indexPath.row];
    
    /**
     * 2、切换主题
     */
    ThemeManager *manager = [ThemeManager defualtManager];
    manager.themeName = [themeName copy];
    
    /**
     * 3、标记选中的主题
     */
    if (_LastSelectedIndex != indexPath.row) {
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        UITableViewCell *lastSelectedcell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_LastSelectedIndex inSection:indexPath.section]];
        lastSelectedcell.accessoryType = UITableViewCellAccessoryNone;
        
        _LastSelectedIndex = indexPath.row;
        

    }
    
    
    [tableView reloadData];
    
}

- (void)viewDidDisappear:(BOOL)animated{
    
    NSUserDefaults *userdefualt = [NSUserDefaults standardUserDefaults];
    [userdefualt setInteger:_LastSelectedIndex forKey:@"lastSelectedIndex"];
    
}










- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
